package com.kitty.mybatis.mybatisv1.binding;

import com.kitty.mybatis.mybatisv1.session.KittySqlSession;

import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * @Description
 * @Author SHEN
 * @Date 2020/10/22
 */
public class KittyMapperProxy implements InvocationHandler, Serializable {
    private static final long serialVersionUID = 6734822536055654218L;

    private final KittySqlSession sqlSession;

    private final Class<?> mapperInterface;

    private final Map<Method,KittyMapperMethod> methodCache;

    public KittyMapperProxy(KittySqlSession sqlSession, Class<?> mapperInterface, Map<Method, KittyMapperMethod> methodCache) {
        this.sqlSession = sqlSession;
        this.mapperInterface = mapperInterface;
        this.methodCache = methodCache;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        if(Object.class.equals(method.getDeclaringClass())){
            return method.invoke(this,args);
        }

        KittyMapperMethod mapperMethod = new KittyMapperMethod(mapperInterface,method,sqlSession.getConfiguration());
        return mapperMethod.execute(sqlSession, args);
    }
}
